<template>
	<view>
		<view class="item" v-for="(item, index) in addressData" :key="index">
			<view class="top">
				<view class="name">{{ item.receiver }}</view>
				<view class="phone">{{ item.phone }}</view>
				<view class="tag">
					<text v-if="item.status === 1" :class="{red:item.status === 1}">默认</text>
				</view>
			</view>
			<view class="bottom">
				{{item.region}}
				<u-icon @click="doUpdate(item.id)" name="edit-pen" :size="40" color="#999999"></u-icon>
			</view>
		</view>
		<!-- 空状态 -->
		<view v-if="isEmpty">
			<u-empty mode="address" icon="http://cdn.uviewui.com/uview/empty/car.png">
			</u-empty>
		</view>
		<view class="addSite" @click="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		onLoad,
		onPullDownRefresh,
	} from "@dcloudio/uni-app";
	import {
		queryData
	} from "@/api/address/index.js";
	import {
		getUserInfo
	} from "@/stores/index.js";
	
	const user = getUserInfo();
	const isEmpty = ref(false);
	const addressData = ref([]);
	onLoad(() => {
		getAddressData();
	})
	
	//获取当前用户的收货地址
	const getAddressData = () => {
		queryData(user.id).then((res) => {
			if(res.code === 200 && res.data !== null){
				addressData.value = res.data;
				isEmpty.value = false;
			}else{
				isEmpty.value = true;
			}
		})
	}
	
	//跳转到新增收货地址页面
	const toAddSite = () => {
		uni.navigateTo({
			url: '/pages/address/addSite/addSite'
		});
	}
	
	//跳转到修改收货地址页面
	const doUpdate = (id) => {
		uni.navigateTo({
			url: "/pages/address/updateSite/updateSite?id=" + id
		})
	}
	
	//下拉刷新请求触发
	onPullDownRefresh(() => {
		getAddressData();
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 800);
	})
</script>

<style lang="scss" scoped>
	.item {
		padding: 40rpx 20rpx;

		.top {
			display: flex;
			font-weight: bold;
			font-size: 34rpx;

			.phone {
				margin-left: 60rpx;
			}

			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;

				text {
					display: block;
					width: 60rpx;
					height: 34rpx;
					line-height: 34rpx;
					color: #ffffff;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 30rpx;
					background-color: rgb(49, 145, 253);
				}

				.red {
					background-color: red
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 20rpx;
			font-size: 28rpx;
			justify-content: space-between;
			color: #999999;
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: absolute;
		bottom: 30rpx;
		left: 80rpx;
		background-color: red;
		border-radius: 60rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>